package com.github.admin.edu.security.jwt.server.service.impl;

import com.fasterxml.jackson.core.type.TypeReference;
import com.github.admin.edu.assembly.common.entity.JsonEntity;
import com.github.admin.edu.assembly.common.util.JsonUtils;
import com.github.admin.edu.assembly.common.util.ObjectUtil;
import com.github.admin.edu.assembly.string.util.StringUtils;
import com.github.admin.edu.orm.serivce.impl.DataRestfulAbstractBaseService;
import com.github.admin.edu.security.jwt.server.domain.ServerUserRoleManagerDomain;
import com.github.admin.edu.security.jwt.server.domain.ServerUserSysLoginDomain;
import com.github.admin.edu.security.jwt.server.entity.TSysRoleUser;
import com.github.admin.edu.security.jwt.server.entity.TSysUser;
import com.github.admin.edu.security.jwt.server.service.ServerUserRoleManagerService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.lang.reflect.Field;
import java.util.List;

/**
 * Created by IntelliJ IDEA.
 * user:wangj
 * date:2018/8/12
 * Time: 19:22
 */
@Service
@Slf4j
public class ServerUserRoleManagerServiceImpl extends DataRestfulAbstractBaseService<TSysRoleUser,Long> implements ServerUserRoleManagerService {

    @Autowired
    private ServerUserRoleManagerDomain domain;


    @Override
    public String getALLPostByPager(String jsonEntity) {
        if(StringUtils.isNotBlank(jsonEntity)){
            JsonEntity<TSysRoleUser> tJsonEntity= JsonUtils.toCollection(jsonEntity,
                    new TypeReference<JsonEntity<TSysRoleUser>>() {
                    });
            if(null!=tJsonEntity){
                return super.getALLPostByPager(tJsonEntity.getData(),tJsonEntity.getPager());
            }
        }
        return JsonUtils.toJson(new JsonEntity<TSysRoleUser>("未找到参数！"));
    }

    @Override
    public String getAllGetEntity(String title, Integer current, Integer rows, String order, String fileId) {

        return super.getAllGetEntity(null,current,rows,order,fileId);
    }

    @Override
    public String getAllEntity(String title, String order, String fileId) {
        return getAllGetEntity(title,0,0,order,fileId);
    }

    @Transactional
    @Override
    public String saveOrUpdate(String jsonEntity) {
        if(StringUtils.isNotBlank(jsonEntity)){
            JsonEntity<TSysRoleUser> tJsonEntity= JsonUtils.toCollection(jsonEntity, new TypeReference<JsonEntity<TSysRoleUser>>() {
            });
            TSysRoleUser t=tJsonEntity.getData();
            Object o= ObjectUtil.getValueByKey(t,"id");
            if(org.springframework.util.StringUtils.isEmpty(o)){
                Field f = null;
                try {
                    f = t.getClass().getDeclaredField("id");
                    f.setAccessible(true);
                    f.set(t,getId(t.getClass().getSimpleName()));
                } catch (NoSuchFieldException e) {
                    log.info("error!!"+e.getMessage());
                    return JsonUtils.toJson(new JsonEntity<TSysRoleUser>("反序列化错误！"));
                } catch (IllegalAccessException e) {
                    log.info("error!!"+e.getMessage());
                    return JsonUtils.toJson(new JsonEntity<TSysRoleUser>("反序列化错误！"));
                }
            }
            t=domain.save(t);
            tJsonEntity.setData(t);
            return JsonUtils.toJson(tJsonEntity);
        }
        return JsonUtils.toJson(new JsonEntity<TSysRoleUser>("保存对象为空！"));
    }

    @Transactional
    @Override
    public String delete(String ids) {
        return super.delete(ids,"Integer");
    }

    @Autowired
    private ServerUserSysLoginDomain loginDomain;

    @Override
    public String getUserRole(String userId, Integer roleId) {
        if(StringUtils.isNotBlank(userId)&&null!=roleId){
            List<TSysUser> list=loginDomain.getByUserid(userId);
            if(null!=list&&list.size()>0){
                TSysUser sysUser=list.get(0);
                if(null!=sysUser){
                    List<TSysRoleUser> roleUsers=domain.getAllByUseridAndRoleid(sysUser.getId(),roleId);
                    if(null!=roleUsers&&roleUsers.size()>0){
                        TSysRoleUser roleUser=roleUsers.get(0);
                        JsonEntity<TSysRoleUser> jsonEntity=new JsonEntity<>();
                        jsonEntity.setData(roleUser);
                        return JsonUtils.toJson(jsonEntity);
                    }
                }
            }
        }
        return JsonUtils.toJson(new JsonEntity<TSysRoleUser>());
    }
}
